package utils.sl651;

import java.util.ArrayList;
import java.util.List;

/**
 * SL651自定义元素标签管理器
 * 用于管理和查询自定义数据元素标签
 */
public class SL651CustomElementLabelManager {
    private static List<SL651CustomElementLabel> customElementLabels = new ArrayList<>();
    
    static {
        // 初始化默认的自定义元素标签
        initializeDefaultLabels();
    }
    
    /**
     * 初始化默认的自定义元素标签
     */
    private static void initializeDefaultLabels() {
        // 渗压数据相关标签
        customElementLabels.add(new SL651CustomElementLabel("FE30", "第一通道渗压水位", "FE", "30", SL651Protocol.SEEPAGE_WATER_LEVEL, "m"));
        customElementLabels.add(new SL651CustomElementLabel("FE20", "第一通道渗压值", "FE", "20", SL651Protocol.SEEPAGE_PRESSURE, "KPa"));
        customElementLabels.add(new SL651CustomElementLabel("FE10", "第一通道渗压温度", "FE", "10", SL651Protocol.SEEPAGE_TEMPERATURE, "℃"));
        customElementLabels.add(new SL651CustomElementLabel("FE00", "第一通道渗压模数", "FE", "00", SL651Protocol.SEEPAGE_MODULUS, "F"));
        
        customElementLabels.add(new SL651CustomElementLabel("FE31", "第二通道渗压水位", "FE", "31", SL651Protocol.SEEPAGE_WATER_LEVEL, "m"));
        customElementLabels.add(new SL651CustomElementLabel("FE21", "第二通道渗压值", "FE", "21", SL651Protocol.SEEPAGE_PRESSURE, "KPa"));
        customElementLabels.add(new SL651CustomElementLabel("FE11", "第二通道渗压温度", "FE", "11", SL651Protocol.SEEPAGE_TEMPERATURE, "℃"));
        customElementLabels.add(new SL651CustomElementLabel("FE01", "第二通道渗压模数", "FE", "01", SL651Protocol.SEEPAGE_MODULUS, "F"));
        
        // 可以继续添加更多自定义标签
    }
    
    /**
     * 根据标识符获取自定义元素标签
     * 
     * @param identifier 标识符（如fe30）
     * @return 自定义元素标签，如果未找到则返回null
     */
    public static SL651CustomElementLabel getLabelByIdentifier(String identifier) {
        for (SL651CustomElementLabel label : customElementLabels) {
            if (label.getLabel().equalsIgnoreCase(identifier)) {
                return label;
            }
        }
        return null;
    }
    
    /**
     * 根据高字节和低字节获取自定义元素标签
     * 
     * @param highByte 高字节
     * @param lowByte 低字节
     * @return 自定义元素标签，如果未找到则返回null
     */
    public static SL651CustomElementLabel getLabelByBytes(String highByte, String lowByte) {
        for (SL651CustomElementLabel label : customElementLabels) {
            if (label.getHighByte().equalsIgnoreCase(highByte) && 
                label.getLowByte().equalsIgnoreCase(lowByte)) {
                return label;
            }
        }
        return null;
    }
    
    /**
     * 获取所有自定义元素标签
     * 
     * @return 自定义元素标签列表
     */
    public static List<SL651CustomElementLabel> getAllLabels() {
        return new ArrayList<>(customElementLabels);
    }
    
    /**
     * 添加自定义元素标签
     * 
     * @param label 自定义元素标签
     */
    public static void addLabel(SL651CustomElementLabel label) {
        customElementLabels.add(label);
    }
    
    /**
     * 移除自定义元素标签
     * 
     * @param label 自定义元素标签
     */
    public static void removeLabel(SL651CustomElementLabel label) {
        customElementLabels.remove(label);
    }
    
    /**
     * 清空所有自定义元素标签
     */
    public static void clearLabels() {
        customElementLabels.clear();
    }
}